package com.ygqh.baby.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgUserLogMapper;
import com.ygqh.baby.po.YgUserLog;
import com.ygqh.baby.po.YgUserLogExample;
import com.ygqh.baby.service.YgUserLogService;

@Service
public class YgUserLogServiceImpl implements YgUserLogService {

	@Autowired
	private YgUserLogMapper ygUserLogMapper;

	@Override
	public List<YgUserLog> find() {
		YgUserLogExample example = new YgUserLogExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygUserLogMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgUserLogExample example = new YgUserLogExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygUserLogMapper.countByExample(example);
	}

	@Override
	public YgUserLog findById(Long id) {
		return ygUserLogMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgUserLog ygUserLog) {
		return ygUserLogMapper.insertSelective(ygUserLog);
	}

	@Override
	public int update(YgUserLog ygUserLog) {
		return ygUserLogMapper.updateByPrimaryKeySelective(ygUserLog);
	}

	@Override
	public ResultSet<YgUserLog> search(QueryInfo queryInfo, String q) {
		List<YgUserLog> list = ygUserLogMapper.selectSuper(queryInfo, q);
		int count = ygUserLogMapper.countSuper(q);
		ResultSet<YgUserLog> resultSet = new ResultSet<YgUserLog>(count, list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgUserLogExample example = new YgUserLogExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgUserLog record = new YgUserLog();
		record.setStatus(DataStatus.Delete);

		return ygUserLogMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(YgUserLog ygUserLog) {
		int r = 0;
		if (ygUserLog.getId() != null) {
			r = this.update(ygUserLog);
		} else {
			r = this.save(ygUserLog);
		}
		return r;
	}

	@Override
	public int addUserLog(Long userId, String operation, String description, String nickName, String remark) {
		YgUserLog log = new YgUserLog(userId, operation, description, nickName, new Date(), remark);
		return this.save(log);
	}

}
